'''
Created on 2009-12-9

@author: Jialai_Zhu
'''
import urllib2
def loop(n):
    try:
        p = n.parent
        while p != None:
            if n.url == p.url:
                return True
            p = p.parent
    except:
        pass
    return False  
def through(n, callback, downloader):
    print n.toString()
    
    if loop(n):
        pass
    else:
        try:
            n.page = downloader.run(n.url, parent=n.parent)
        except urllib2.HTTPError, e: 
            n.page = str(e.code)
            n.code = str(e.code)
        except:
            n.page = ''
            n.code = 'ffff'
            pass    
        
    childs = []
    if n.code == '200':    
        childs = n.get_child()
    
#    print n.toString()
    if callback != None:
        for cb in callback: cb(n)
    checked = []         
    for child in childs:
        ignore = False
        for i in checked:
            if i.url == child.url:
                ignore = True
                break
        if ignore:
            pass
        else:
            try:
                checked.append(child)
                through(child, callback, downloader)
            except Exception, ex:
                print Exception, ':', ex      
